package MathTest.Calculate;

import MathTest.Question.Test;

import java.util.Stack;

public class suffixToArithmetic {
    suffixToArithmetic(){}
    public static int suffixToArithmetic(String exp) {
        // 将后缀表达式分割成字符串数组,此处直接使用空白也可以对字符串进行分割！！
        String[] strings = exp.split(" ");
        Stack<Integer> stack = new Stack<Integer>();
        for (int i = 0; i < strings.length; i++) {
            switch (strings[i]){
                case "+":
                case "-":           // 如果是运算符，则弹出栈顶的两个数进行计算
                case "*":
                case "÷":
                    // ！！！这里需要注意，先弹出的那个数其实是第二个计算数值，这里记作y！
                    int y = stack.pop();
                    int x = stack.pop();
                    // 将运算结果重新压栈
                    stack.push(calculate(x, y, strings[i]));
                    break;
                default:stack.push(Integer.parseInt(strings[i]));   // 如果遇到了数字则直接进栈
            }
        }
        // 弹出栈顶元素就是最终结果
        return stack.pop();
    }
    private static Integer calculate(int x, int y, String string) {
        if (string.equals("+")) {
            return x + y;
        }
        if (string.equals("-")) {
            return x - y;
        }
        if (string.equals("*")) {
            return x * y;
        }
        if (string.equals("÷")) {
            return x / y;
        }
        return 0;
    }
    public static String suffixToArithmetic1(String exp) {
        // 将后缀表达式分割成字符串数组,此处直接使用空白也可以对字符串进行分割！！
        String[] strings = exp.split(" ");
        Stack<String> stack = new Stack<String>();
        for (int i = 0; i < strings.length; i++) {
            switch (strings[i]) {
                case "+":
                case "-":           // 如果是运算符，则弹出栈顶的两个数进行计算
                case "*":
                case "÷":
                    // ！！！这里需要注意，先弹出的那个数其实是第二个计算数值，这里记作y！
                    String y = stack.pop();
                    String x = stack.pop();
                    // 将运算结果重新压栈
                    stack.push(Fraction(x, y, strings[i]));
                    break;
                default:
                    stack.push(strings[i]);   // 如果遇到了数字则直接进栈
            }
        }
        // 弹出栈顶元素就是最终结果
        return stack.pop();
    }

    public static String Fraction(String s1,String s2,String op){
        String s = "";
        String []str1 = new String[100];
        String []str2 = new String[100];
        str1 = null;
        str2 = null;
        int Numerator1=0,Denominator1=0;
        int Numerator2=0,Denominator2=0;
        if(s1.equals("1")&&(s2.equals("1")==false)){
            Numerator1 = 1;
            Denominator1 = 1;
            str2 = s2.split("/");
            Numerator2 = Integer.parseInt(str2[0]);
            Denominator2 = Integer.parseInt(str2[1]);
        }
        else if(s2.equals("1")&&(s1.equals("1")==false)){
            Numerator2 = 1;
            Denominator2 = 1;
            str1 = s1.split("/");
            Numerator1 = Integer.parseInt(str1[0]);
            Denominator1 = Integer.parseInt(str1[1]);
        }
        else if(s1.equals("1")&&s2.equals("1")){
            Numerator1 = 1;
            Denominator1 = 1;
            Numerator2 = 1;
            Denominator2 = 1;
        }
        else{
            if((s1.contains("/")==true)&&(s2.contains("/")==false)){
                str1 = s1.split("/");
                Numerator2 = Integer.parseInt(s2);
                Denominator2 = 1;
                Numerator1 = Integer.parseInt(str1[0]);
                Denominator1 = Integer.parseInt(str1[1]);
            }
            else if((s2.contains("/")==true)&&(s2.contains("/")==false)){
                Numerator1 = Integer.parseInt(s1);
                Denominator1 = 1;
                str2 = s2.split("/");
                Numerator2 = Integer.parseInt(str2[0]);
                Denominator2 = Integer.parseInt(str2[1]);
            }
            else if((s2.contains("/")==false)&&(s2.contains("/")==false)){
                Numerator1 = Integer.parseInt(s1);
                Denominator1 = 1;
                Numerator2 = Integer.parseInt(s2);
                Denominator2 = 1;
            }
            else{
                str1 = s1.split("/");
                str2 = s2.split("/");
                Numerator1 = Integer.parseInt(str1[0]);
                Denominator1 = Integer.parseInt(str1[1]);
                Numerator2 = Integer.parseInt(str2[0]);
                Denominator2 = Integer.parseInt(str2[1]);
            }
        }
        int newDenominator=0;
        int newNumerator=0;
        int gcd;
        switch (op){
            case "+":
                newDenominator = Denominator1*Denominator2;
                newNumerator = Numerator1*Denominator2+Numerator2*Denominator1;
                gcd = Test.GCD(newNumerator,newDenominator);
                break;
            case "-":
                newDenominator = Denominator1*Denominator2;
                newNumerator = Numerator1*Denominator2-Numerator2*Denominator1;
                break;
            case "*":
                newDenominator = Denominator1*Denominator2;
                newNumerator = Numerator1*Numerator2;
                break;
            case "÷":
                newNumerator = Numerator1*Denominator2;
                newDenominator = Numerator2*Denominator1;
                if(newDenominator==0){
                    System.out.println("有bug，不做了");
                    System.exit(0);
                }



        }
        gcd = Test.GCD(Math.abs(newNumerator),Math.abs(newDenominator));
        if(gcd!=1){
            newNumerator = newNumerator/gcd;
            newDenominator = newDenominator/gcd;
        }
        if(newDenominator==1)
            s = Integer.toString(newNumerator);
        else
            s = newNumerator+"/"+newDenominator;
        return s;
    }

}
